package com.jeady.jxcompose

import android.os.CountDownTimer
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.interaction.MutableInteractionSource
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.BoxWithConstraints
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxHeight
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.heightIn
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.layout.wrapContentSize
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.drawBehind
import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.compose.ui.zIndex

private var showing by mutableStateOf(false)
private var secondsToClose by mutableStateOf(0)
private var cancelText by mutableStateOf("")
private var confirmText by mutableStateOf("")
private var text by mutableStateOf("")
private var onDismiss:((Boolean)->Unit)? by mutableStateOf(null)
@Composable
fun TipDisplay() {
    if(showing){
        AlertDlg(text,
            showCancel = cancelText.isNotEmpty(), cancelText = cancelText,
            showConfirm = confirmText.isNotEmpty(), confirmText = confirmText,
            scheduleCloseSeconds=secondsToClose
        ){
            showing = false
            onDismiss?.invoke(it)
        }
    }
}
fun showTip(tip: String, cancelButtonText: String="", confirmButtonText: String="确定",
            secondToClose:Int=0, callback: ((Boolean)->Unit)?=null
) {
    secondsToClose = secondToClose
    text = tip
    onDismiss = callback
    showing = true
    cancelText = cancelButtonText
    confirmText = confirmButtonText
}

@Composable
fun AlertDlg(
    text: String, modifier: Modifier = Modifier, title: String = "",
    confirmText: String = "确定", cancelText: String = "取消",
    showConfirm: Boolean = true, showCancel: Boolean = true,
    scheduleCloseSeconds: Int = 0,
    defaultCancel: Boolean = true,
    onDismiss: (isConfirm: Boolean) -> Unit
) {
    val interactionSource = remember { MutableInteractionSource() }
    Box(modifier = modifier
        .zIndex(999F)
        .fillMaxSize()
        .clickable(interactionSource, null) {}
        .background(Color(0xa0000000)),
        contentAlignment = Alignment.Center
    ) {
        val shape = RoundedCornerShape(10.dp)
        Surface(
            modifier = Modifier
                .wrapContentSize(),
//            .background(MaterialTheme.colorScheme.onPrimary),
            shape = shape,
            shadowElevation = 1.dp
        ) {
            Column(
                Modifier,
                horizontalAlignment = Alignment.CenterHorizontally,
                verticalArrangement = Arrangement.SpaceEvenly
            ) {
                // 显示标题
                if (title.isNotBlank()) {
                    Text(
                        text = title,
                        Modifier.height(50.dp),
                        fontSize = 30.sp,
                        fontWeight = FontWeight.SemiBold,
                        color = Color.Gray
                    )
                }
                // 显示内容
                BoxWithConstraints {
                    LazyColumn(
                        Modifier
                            .fillMaxWidth(0.6f)
                            .heightIn(100.dp, maxHeight * 0.7f)
                            .padding(10.dp),
                        horizontalAlignment = Alignment.CenterHorizontally,
                        verticalArrangement = Arrangement.Center
                    ) {
                        item {
                            Text(
                                text = text,
                                fontSize = 18.sp
                            )
                        }
                    }
                }
                // 显示操作按钮
                Row(
                    Modifier
                        .fillMaxWidth(0.5f)
                        .height(50.dp)
                        .drawBehind {
                            drawLine(Color.Gray, Offset(0f, 0f), Offset(size.width, 0f))
                        },
                    verticalAlignment = Alignment.CenterVertically
                ) {
                    if (showCancel) {
                        Box(
                            modifier = Modifier
                                .fillMaxHeight()
                                .weight(1f)
                                .clickable {
                                    onDismiss(false)
                                },
                            contentAlignment = Alignment.Center
                        ) {
                            Row(verticalAlignment = Alignment.CenterVertically) {
                                Text(text = cancelText, fontSize = 20.sp)
                                if (scheduleCloseSeconds > 0 && defaultCancel) {
                                    var counter by remember { mutableStateOf(scheduleCloseSeconds) }
                                    Text("(${counter}s)")
                                    LaunchedEffect(text) {
                                        object :
                                            CountDownTimer(scheduleCloseSeconds * 1000L, 1000) {
                                            override fun onTick(millisUntilFinished: Long) {
                                                --counter
                                            }

                                            override fun onFinish() {
                                                onDismiss(false)
                                            }
                                        }.start()
                                    }
                                }
                            }
                        }
                    }
                    if (showConfirm && showCancel) {
                        Spacer(
                            modifier = Modifier
                                .fillMaxHeight(0.6f)
                                .width(2.dp)
                                .background(Color.Gray)
                        )
                    }
                    if (showConfirm) {
                        Box(
                            modifier = Modifier
                                .fillMaxHeight()
                                .weight(1f)
                                .clickable {
                                    onDismiss(true)
                                },
                            contentAlignment = Alignment.Center
                        ) {
                            Row(verticalAlignment = Alignment.CenterVertically) {
                                Text(text = confirmText, fontSize = 20.sp)
                                if (scheduleCloseSeconds > 0 && (!defaultCancel || !showCancel)) {
                                    var counter by remember {
                                        mutableStateOf(
                                            scheduleCloseSeconds
                                        )
                                    }
                                    Text("(${counter}s)")
                                    LaunchedEffect(text) {
                                        object :
                                            CountDownTimer(scheduleCloseSeconds * 1000L, 1000) {
                                            override fun onTick(millisUntilFinished: Long) {
                                                --counter
                                            }

                                            override fun onFinish() {
                                                onDismiss(true)
                                            }
                                        }.start()
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}